package com.fecean.aabbc.netty.client.init;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * NettyClientHandler
 *
 * @author: chenfenghai
 * @version: 1.0
 * @date: 2020-02-24 10:11
 * @blame Team
 */
@Slf4j
@Component
public class NettyClientHandler extends ChannelInboundHandlerAdapter {

    @Resource
    ClientChannel clientChannel;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("客户端Active .....");
        clientChannel.saveClientChannel(ctx.channel());
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        log.info("客户端收到消息: {}", msg.toString());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("", cause);
        ctx.close();
    }
}
